/**
 * Android FriendFeed Client
 * Copyright (C) 2010 E & B Labs
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.eblabs.androidfriendfeedclient;

import android.graphics.Bitmap;
import android.os.Bundle;
import java.io.IOException;
import java.util.ArrayList;
import net.eblabs.friendfeed.ApiException;
import net.eblabs.friendfeed.UnAuthorizedException;
import net.eblabs.friendfeed.FriendFeedAPI;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Representación de entrada en FriendFeed.
 * @author Ivan (epplestun@gmail.com)
 */
public class FriendfeedEntry {
    /**
     * Cuerpo de la entrada.
     * @type   String
     * @access private
     */
    private String body;
    /**
     * Fecha de la entrada.
     * @type   String
     * @access private
     */
    private String date;
    /**
     * Emisor de la entrada.
     * @type   String
     * @access private
     */
    private String from;
    /**
     * Identificador de usuario.
     * @type   String
     * @access private
     */
    private String fromId;
    /**
     * Lugar de envío de la entrada.
     * @type   String
     * @access private
     */
    private String via;
    /**
     * Identificador de entrada
     * @type   Stirng
     * @access private
     */
    private String id;
    /**
     * Indica el número de comentarios que tiene la entrada.
     * @type   int
     * @access private
     */
    private int nComments;
    /**
     * Almacena los comentarios de la entrada.
     * @type   JSONObject
     * @access private
     */
    private JSONArray comments;
    /**
     * Imagen del usuario.
     * @type   Bitmap
     * @access private
     */
    private Bitmap picture;
    /**
     * Indica las personas a las que le gusta la entrada
     */
    private ArrayList<String> likes;

    /**
     * Recupera el cuerpo de la entrada.
     * @access public
     * @return String
     */
    public String getId() {
        return id;
    }

    /**
     * Indica el cuerpo de la entrada.
     * @param  body
     * @access public
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * Recupera el cuerpo de la entrada.
     * @access public
     * @return String
     */
    public String getBody() {
        return body;
    }

    /**
     * Indica el cuerpo de la entrada.
     * @param  body
     * @access public
     */
    public void setBody(String body) {
        this.body = body;
    }

    /**
     * Recupera la fecha de la entrada.
     * @access public
     * @return String
     */
    public String getDate() {
        return date;
    }

    /**
     * Indica la fecha de la entrada.
     * @param  date
     * @access public
     */
    public void setDate(String date) {
        this.date = date;
    }

    /**
     * Recupera el emisor de la entrada.
     * @access public
     * @return String
     */
    public String getFrom() {
        return from;
    }

    /**
     * Indica el emisor de la entrada.
     * @param  from
     * @access public
     */
    public void setFrom(String from) {
        this.from = from;
    }

    /**
     * Recupera el emisor de la entrada.
     * @access public
     * @return String
     */
    public String getFromId() {
        return fromId;
    }

    /**
     * Indica el emisor de la entrada.
     * @param  from
     * @access public
     */
    public void setFromId(String fromId) {
        this.fromId = fromId;
    }

    /**
     * Recupera el lugar de envío de la entrada.
     * @access public
     * @return String
     */
    public String getVia() {
        return via;
    }

    /**
     * Indica el lugar de envío de la entrada.
     * @param  via
     * @access public
     */
    public void setVia(String via) {
        this.via = via;
    }

    /**
     * Recupera el número de comentarios de la entrada.
     * @access public
     * @return int
     */
    public int getNComments() {
        return nComments;
    }

    /**
     * Indica el número de comentarios de la entrada.
     * @param  nComments
     * @access public
     */
    public void setNComments(int nComments) {
        this.nComments = nComments;
    }

    /**
     * Recupera el número de comentarios de la entrada.
     * @access public
     * @return int
     */
    public JSONArray getComments() {
        return comments;
    }

    /**
     * Indica el número de comentarios de la entrada.
     * @param  JSONArray comments
     * @access public
     */
    public void setComments(JSONArray comments) {
        this.comments = comments;
    }

    /**
     * Recupera el número de comentarios de la entrada.
     * @access public
     * @return int
     */
    public Bitmap getPicture() throws IOException, UnAuthorizedException,
            ApiException {
        picture = FriendFeedAPI.getInstance().getUserPicture(getFromId(), "medium");

        return picture;
    }

    public Bundle toBundle() {
        Bundle bundle = new Bundle();
        bundle.putString("id", this.getId());
        bundle.putString("body", this.getBody());
        bundle.putString("date", this.getDate());
        bundle.putString("from", this.getFrom());
        bundle.putString("fromId", this.getFromId());
        bundle.putString("via", this.getVia());
        bundle.putString("comments", this.getComments().toString());
        //bundle.putBundle("comments", this.getComments());

        return bundle;
    }

    public boolean userLikesEntry(String username) {
        if(this.likes == null)
            return false;
        
        return this.likes.contains(username);
    }

    /**
     * Constructor por defecto.
     */
    public FriendfeedEntry() {
        super();
    }

    /**
     * Constructor basandose en un Entry JSON.
     * @param item Entry JSON.
     * @throws JSONException Excepción en caso de error.
     */
    public FriendfeedEntry(JSONObject item) throws JSONException {
        this.likes = new ArrayList<String>();
        
        this.setBody(
                item.getString("body").toString().replaceAll("\\<.*?>", ""));
        this.setFrom(
                item.getJSONObject("from").getString("name"));
        this.setFromId(
                item.getJSONObject("from").getString("id"));
        this.setDate(
                item.getString("date"));
        this.setId(
                item.getString("id"));

        if (item.isNull("comments")) {
            this.setNComments(0);
        } else {
            this.setComments(item.getJSONArray("comments"));
            this.setNComments(item.getJSONArray("comments").length());
        }

        if (!item.isNull("likes")) {            
            JSONArray likesArray = item.getJSONArray("likes");

            for(int i = 0; i < likesArray.length(); i++) {
                JSONObject obj = likesArray.getJSONObject(i).getJSONObject("from");
                this.likes.add(obj.getString("id"));
            }
        } else {
            this.likes = null;
        }
    }

    public static ArrayList<FriendfeedEntry>
            entriesToArrayList(JSONArray entries) throws JSONException {
        ArrayList<FriendfeedEntry> result = new ArrayList<FriendfeedEntry>();

            for (int i = 0; i < entries.length(); i++) {
                FriendfeedEntry ffEntry = new FriendfeedEntry(
                        entries.getJSONObject(i));
                result.add(ffEntry);
            }

            return result;
    }
}
